#include <bits/stdc++.h>
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
using namespace std;
const int N = 2e5 + 10;
int n, m, p[N], vis[N], cnt[N], ans, tans[2], x;
int main() {
	cin >> n >> m;
	rep(i, 1, n + m) cin >> p[i];
	rep(i, 1, n + m) if(!vis[x = i]){
		cnt[0] = cnt[1] = 0;
		while(!vis[x]) vis[x] = 1, cnt[x > n]++, ans++, x = p[x];
		ans--; if(cnt[0] + cnt[1] == 1) continue; 
		if(cnt[0] == 0 || cnt[1] == 0) tans[cnt[1] == 0]++;
	} printf("%d\n", ans + 2 * max(tans[0], tans[1]));
	return 0;
}
